package com.example.localdb;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.localdb.util.DebugLog;
import com.example.localdb.util.PermissionListener;
import com.example.localdb.util.PermissionUtil;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button btnImage;
    private Button btnTv;
    private Button btnSql;
    private ImageView ivImage;
    private ImageView ivface;
    public   String AUTHORITY = "cn.app.hscserver.provider.DBContentProvider";//地址
    public   Uri CONTENT_FACE_URI = Uri.parse("content://" + AUTHORITY + "/" + "FACE_INFO_BEAN");//Uri所对应的资源
    public   Uri CONTENT_PIC_URI = Uri.parse("content://" + AUTHORITY + "/" + "HSC_PIC_BEAN");//Uri所对应的资源
    private String url ;
    private String picUrl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnImage = findViewById(R.id.btn_image);
        btnTv = findViewById(R.id.btn_tv);
        btnSql = findViewById(R.id.btn_sql);
        ivImage = findViewById(R.id.iv_image);
        ivface = findViewById(R.id.iv_face);

//        initData();

        // 获取ContentResolver
        ContentResolver resolver =  getContentResolver();
        btnImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 通过ContentResolver 向ContentProvider中查询数据
                String selection = "Md5='3ca06e05d0d54bff2dbfeb6b2b4cf849'";
                Cursor cursor = resolver.query(CONTENT_PIC_URI, new String[]{"FILE_COMPRESS_PATH"}, selection, null, null);
                if(cursor!=null){
                    while (cursor.moveToNext()){
                        picUrl= cursor.getString(0);
                        DebugLog.d("PROVIDER" +" query :" + cursor.getString(0));
                        // 将表中数据全部输出
                    }
                    cursor.close();
                }

                setImage(picUrl,ivImage);
            }
        });
        btnTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 通过ContentResolver 向ContentProvider中查询数据
//                String selection = "FIGURE!='defult' and FIGURE!='0' and FIGURE !='1' group by figure,file_head_path";
//                String selection = "not (FIGURE='defult')and not (FIGURE='0') and not (FIGURE='1') group by figure,file_head_path having figureCount >10 order by figureCount desc" ;
                //排序--
                String selection = "not (FIGURE='defult')and not (FIGURE='0') and not (FIGURE='1') group by figure,file_head_path order by figureCount desc " ;
//                Cursor cursor = resolver.query(CONTENT_FACE_URI, new String[]{"figure","file_head_path"}, selection, null, null);
                Cursor cursor = resolver.query(CONTENT_FACE_URI, new String[]{"figure","file_head_path","count(figure) figureCount"}, selection, null, null);
                if(cursor!=null){
                    while (cursor.moveToNext()){
                        url= cursor.getString(1);
                        DebugLog.d("PROVIDER" +" query :" + cursor.getString(0) +" ---   "+ cursor.getString(1)+" --  "+cursor.getString(2));
                        // 将表中数据全部输出
                    }
                    cursor.close();
                }

                // 关闭游标
             setImage(url,ivface);
            }
        });
        btnSql.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 通过ContentResolver 向ContentProvider中查询数据
//                String selection = "FIGURE!='defult' and FIGURE!='0' and FIGURE !='1' group by figure,file_head_path";
//                String selection = "not (FIGURE='defult')and not (FIGURE='0') and not (FIGURE='1') group by figure,file_head_path having figureCount >10 order by figureCount desc" ;
                //排序--
                String selection ="md5 in (select distinct(face_md5) from FACE_INFO_BEAN where figure in " +
                        "(select figure from FACE_INFO_BEAN where figure != 1 group by figure order by count(1) desc limit ? )) and take_year= ? order by take_date_time" ;
//                Cursor cursor = resolver.query(CONTENT_FACE_URI, new String[]{"figure","file_head_path"}, selection, null, null);
                Cursor cursor = resolver.query(CONTENT_PIC_URI, new String[]{"file_name","md5","file_compress_path","take_date_time","take_date","district","figure_count"}, selection, new String[]{"1","2012"}, null);
                if(cursor!=null){
                    while (cursor.moveToNext()){
                        url= cursor.getString(1);
                        DebugLog.d("PROVIDER" +" query :" + cursor.getString(0) +" ---   "+ cursor.getString(1)+" --  "+cursor.getString(2));
                        // 将表中数据全部输出
                    }
                    cursor.close();
                }
            }
        });

//        String getPic_sql = "select file_name,md5,file_compress_path,take_date_time,take_date,district,figure_count from HSC_PIC_BEAN where md5 in (select distinct(face_md5) from FACE_INFO_BEAN where figure in " +
//                        "(select figure from FACE_INFO_BEAN where figure != 1 group by figure order by count(1) desc limit ? )) and take_year= ? order by take_date_time ";

    }

    /**
     * 显示图片
     *
     * @param headPath  人脸图片
     * @param imageView 控件
     */
    public void setImage(String headPath, ImageView imageView) {
        Bitmap bm = BitmapFactory.decodeFile(headPath);
        imageView.setImageBitmap(bm);
    }

//    private void initData() {
//        PermissionUtil.requestPermission(this, new PermissionListener() {
//            @Override
//            public void onGranted() {
//                // 设置URI
//
//            }
//
//            @Override
//            public void onDenied(List<String> deniedPermissions) {
//
//            }
//        });
//
//    }

}
